//
//  XDSelectGradeViewController.m
//  XDEducation
//
//  Created by apple on 2017/4/14.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDSelectGradeViewController.h"
#import "XDSelectCourseData.h"
#import "XDSingleBtnCell.h"
#import "XDSelectCourseItemCell.h"
#import "XDSelectUnitViewController.h"
@interface XDSelectGradeViewController ()
/** 页面数据 */
@property(nonatomic,strong) NSMutableArray *selectGradeData;

//跳转下个页面带入的数据
@property(nonatomic,assign)NSInteger gradeId;     //年级ID
@property(nonatomic,assign)NSInteger termId;      //学期ID
@end

static NSString *const selectCourseCellId = @"seletGradeCell";

@implementation XDSelectGradeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _gradeId = 0;
    _termId = 0;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


-(NSMutableArray *)selectGradeData{
    
    if (!_selectGradeData) {
        
        _selectGradeData = [NSMutableArray array];
        _selectGradeData = [XDSelectCourseData getGradeCourseData];
    }
    return _selectGradeData;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.selectGradeData.count + 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == self.selectGradeData.count) return 0;
    
    return 44;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 3) return nil;
    
    
    NSString *title  = self.selectGradeData[section][@"name"];
    
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 200, 20)];
    label.textColor = UIColorFromRGB(0x333333);
    label.font = [UIFont systemFontOfSize:16];
    label.text = title;
    [view addSubview:label];
    return view;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.section) {
        case 0:
            return 140 * ScaleHeight;
            break;
        case 1:
            return 80 * ScaleHeight;
            break;
        case 2:
            return 50 * ScaleHeight;
            break;
        default:
            return 0;
            break;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WeakSelf(weakself)
    if (indexPath.section == self.selectGradeData.count) {   //最后一行的button
        XDSingleBtnCell *cell =  [XDSingleBtnCell cellWithTableView:tableView];
        
        cell.buttonClickedBlock = ^(){
            
            if (weakself.gradeId == 0){
                [MBProgressHUD showError:@"请选择年级"];
                return ;
            }
            if (weakself.termId == 0){
                [MBProgressHUD showError:@"请选择学期"];
                return ;
            }
            
            XDSelectUnitViewController *vc = [[XDSelectUnitViewController alloc]init];
            vc.title = weakself.title;
            vc.gradeId = weakself.gradeId;
            vc.termId = weakself.termId;
            if ([weakself.title isEqualToString:@"汉字练习"]) {
                vc.type = PracticeTypeChinese;
            }else{
                vc.type = PracticeTypeEnglish;

            }
            [weakself.navigationController pushViewController:vc animated:YES];
            
        };
        return cell;
        
    }else{  //1 2 3 组cell
        
        
        NSArray *item  = self.selectGradeData[indexPath.section][@"item"];
        
        XDSelectCourseItemCell *cell = [XDSelectCourseItemCell cellWithTableView:tableView itemDataArr:item cellId:selectCourseCellId];
        cell.itemClickdedBlock = ^(NSInteger tag,BOOL selectStatus){
            
            if (indexPath.section == 0){  //年级
                //1-6代表一到六年级
                
                if (selectStatus) {
                    weakself.gradeId = tag + 1;
                }else{
                    weakself.gradeId = 0;
                }
                
            }else if(indexPath.section == 1){   //学期
                
                if (selectStatus) {
                    weakself.termId = tag + 1;
                }else{
                    weakself.termId = 0;
                }
                
            }
            
            
        };
        return cell;
    }
}


@end
